package com.songcha.library_business.helper.permission

import android.Manifest
import android.app.Activity
import android.app.Dialog
import android.content.Context
import android.os.Build
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import com.songcha.library_common.helper.PermissionTipHelper
import com.songcha.library_common.helper.SharedPreferenceHelper
import com.tbruyelle.rxpermissions3.RxPermissions

class SaveStoragePermissionHelper {
    companion object{
        private var mPermissions: RxPermissions?= null

        @JvmStatic
        fun <T> hasPermission(t: T,block:(Int)->Unit){
            var activity: Activity? = null
            if(t is Fragment){
                mPermissions = RxPermissions(t)
                activity = t.requireActivity()
            }else if(t is FragmentActivity){
                mPermissions = RxPermissions(t)
                activity = t
            }

            if(activity == null) return

            val permission = if(Build.VERSION.SDK_INT >= 33) Manifest.permission.READ_MEDIA_IMAGES else Manifest.permission.WRITE_EXTERNAL_STORAGE
            mPermissions?.shouldShowRequestPermissionRationale(activity,permission)
                ?.subscribe ({
                    val isGranted = mPermissions?.isGranted(permission)?:false
                    if(isGranted){
                        block(PermissionResult.PERMISSION_GRANTED)
                    }else{
                        if(it){
                            block(PermissionResult.PERMISSION_DENY)
                        }else{
                            if(SharedPreferenceHelper.getBoolean("permission","storage")){
                                block(PermissionResult.PERMISSION_FOREVER_DENY)
                            }else{
                                block(PermissionResult.PERMISSION_ASK)
                            }
                        }
                    }

                },{

                })

        }

        @JvmStatic
        fun <T> request(t: T,tip:String,onHasPermission:(isGranted:Boolean)->Unit,onPermissionDeny: (() -> Unit)){
            var context: Context? = null

            if(t is Fragment)
                context = t.requireContext()
            else if(t is FragmentActivity)
                context = t
            if(context==null){
                return
            }

            hasPermission(t){pm->
                when(pm){
                    PermissionResult.PERMISSION_ASK->{
                        var dialogTip: Dialog? = null
                        if(context != null){
                            dialogTip = PermissionTipHelper.showPermissionTip(context,tip)
                        }

                        SharedPreferenceHelper.setBoolean("permission","storage",true)

                        mPermissions?.request(if(Build.VERSION.SDK_INT >= 33) Manifest.permission.READ_MEDIA_IMAGES else Manifest.permission.WRITE_EXTERNAL_STORAGE)
                            ?.subscribe {
                                dialogTip?.dismiss()
                                if (it) {
                                    onHasPermission(true)
                                } else {
                                    onPermissionDeny()
                                }
                            }
                    }
                    PermissionResult.PERMISSION_GRANTED ->{
                        onHasPermission(false)
                    }
                    PermissionResult.PERMISSION_DENY, PermissionResult.PERMISSION_FOREVER_DENY ->{
                        onPermissionDeny()
                    }
                }
            }

        }

        @JvmStatic
        fun destroy(){
            mPermissions = null
        }

    }
}